package com.atguigu.gmall.task.service;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.task.entity.GmallCorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.UUID;

/**
 * @author: 李旭
 * @create: 2021-07-24 14:15
 *
 *  提供服务
 *    RabbitMQ发送服务  （封装好服务）
 **/
@SuppressWarnings("all")
@Component
public class RabbitService {


    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private RedisTemplate redisTemplate;

    //发消息
    public boolean sendMessage(String exchange,String routingKey,Object msg){
        GmallCorrelationData gmallCorrelationData = new GmallCorrelationData();
        //uuid
        String uuid = UUID.randomUUID().toString();
        gmallCorrelationData.setId(uuid);
        //交换机
        gmallCorrelationData.setExchange(exchange);
        //路由关键词
        gmallCorrelationData.setRoutingKey(routingKey);
        //消息
        gmallCorrelationData.setMessage(msg);
        //保存到缓存中
        redisTemplate.opsForHash().put("gmallCorrelationData",uuid,
                JSONObject.toJSONString(gmallCorrelationData));//json转换异常
        //发消息
        rabbitTemplate.convertAndSend(exchange,routingKey,msg,gmallCorrelationData);
        return true;
    }
    //发送死信消息  参数4：delayTime 表示秒
    public void sendDeadLetterMessage(String exchange,String routingKey,Object msg,int delayTime){
        GmallCorrelationData gmallCorrelationData = new GmallCorrelationData();
        //uuid
        String uuid = UUID.randomUUID().toString();
        gmallCorrelationData.setId(uuid);
        //交换机
        gmallCorrelationData.setExchange(exchange);
        //路由关键词
        gmallCorrelationData.setRoutingKey(routingKey);
        //消息
        gmallCorrelationData.setMessage(msg);
        //是延迟消息
        gmallCorrelationData.setDelay(true);
        gmallCorrelationData.setDelayTime(delayTime);

        //保存到缓存中
        redisTemplate.opsForHash().put("gmallCorrelationData",uuid,
                JSONObject.toJSONString(gmallCorrelationData));//json转换异常
        //发消息
        rabbitTemplate.convertAndSend(exchange,routingKey,msg,message -> {
            //过期时间  死信 TTL  time to live    毫秒
            message.getMessageProperties().setExpiration(String.valueOf(delayTime*1000));
            System.out.println("发死信的时间：" + new Date());
            return message;
        },gmallCorrelationData);
    }
    //发送延迟消息
    public void sendDelayMessage(String exchange,String routingKey,Object msg,int delayTime){
        GmallCorrelationData gmallCorrelationData = new GmallCorrelationData();
        //uuid
        String uuid = UUID.randomUUID().toString();
        gmallCorrelationData.setId(uuid);
        //交换机
        gmallCorrelationData.setExchange(exchange);
        //路由关键词
        gmallCorrelationData.setRoutingKey(routingKey);
        //消息
        gmallCorrelationData.setMessage(msg);
        //是延迟消息
        gmallCorrelationData.setDelay(true);
        gmallCorrelationData.setDelayTime(delayTime);

        //保存到缓存中
        redisTemplate.opsForHash().put("gmallCorrelationData",uuid,
                JSONObject.toJSONString(gmallCorrelationData));//json转换异常
        //发消息
        rabbitTemplate.convertAndSend(exchange,routingKey,msg,message -> {
            //过期时间  死信 TTL  time to live    毫秒
            message.getMessageProperties().setDelay(delayTime*1000);
            System.out.println("发延迟的时间：" + new Date());
            return message;
        },gmallCorrelationData);
    }
}
